import Axios from 'axios'

const state = {
    loading: [true],
    treeData: [],           //树形结构数据
    tableData: [],          //表格数据
    expandNodeId: [],
    row: [],                //当前展开的节点
    rowId: null,
    multipleSelection: [],      //选中的表格节点数据
    selectRowId: [],            //选中的表格节点数据id
    parentId: [],           //分管部门id
    parentName: [],         //分管部门名称
    searchResult: [],       //搜索数据
}

const mutations = {
    updateLoading(state, bool) {
        state.loading.splice(0, 1, bool);
    },
    updateExpandNodeId(state, id) {     //更新展开节点
        if(id) {
            state.expandNodeId.splice(0, 1, id);
        } else {
            state.expandNodeId.splice(0);
        }
    },
    updateRowId(state, id) {
        state.rowId = id ? id : null;
    },
    updateRow(state, row) {
        state.row.splice(0, 1, row);
    },
    updateParentId(state, id) {             //更新分管部门id
        state.parentId.splice(0, 1, id);
    },
    updateParentName(state, name) {             //更新分管部门名称
        state.parentName.splice(0, 1, name);
    },
    updateTableData(state, arr) {           //更新表格数据
        let treeData = state.treeData,
            tableData = state.tableData,
            expandNodeId = state.expandNodeId[0];
        if(arr) {
            if(arr.length > 0) {
                tableData.length = 0;
                arr.forEach(item => tableData.push(item));
            } else {
                tableData.splice(0);
            }
        } else {
            treeData.forEach(item => {
                if(item['id'] == expandNodeId) {
                    if(item['clients'] && item['clients'].length > 0) {
                        tableData.length = 0;
                        item['clients'].forEach(item => tableData.push(item));
                    } else {
                        tableData.splice(0);
                    }
                }
            });
        }
    },
    updateTableData2(state, arr) {
        let tableData = state.tableData;
        tableData.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => tableData.push(item));
        } else {
            tableData.splice(0);
        }
    },
    multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    }
}

const actions = {
    getTreeData({state, commit, dispatch, rootState}) {
        state.loading.splice(0, 1, true);
        Axios.get('client/findAccountItemByName').then(res => {
            if(res.data.code == 20001) {
                setTimeout(() => {
                    state.loading.splice(0, 1, false);
                }, 300);
                if(res.data.data) {
                    let treeData = state.treeData,
                        data = res.data.data;
                    treeData.length = 0;
                    if(state.expandNodeId.length == 0) {
                        state.expandNodeId.splice(0, 1, data[0]['id']);
                        state.row.splice(0, 1, data[0]);
                    }
                    data.forEach(item => treeData.push(item));
                    commit('updateTableData');          //更新表格数据
                }
            } else {
                setTimeout(() => {
                    state.loading.splice(0, 1, false);
                }, 300);
            }
        });
        dispatch('getSearchData');
    },
    getSearchData({state}) {        //搜索数据
        Axios.get('client/fuzzySearch').then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    let searchResult = state.searchResult,
                        data = res.data.data;
                    searchResult.length = 0;
                    data.forEach(item => searchResult.push(item));
                }
            }
        });
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}